package com.example.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.domain.Person;

public class PersonService {
	private DBOpenHelper dbopenhelper;
	public PersonService(Context context)
	{
		this.dbopenhelper=new DBOpenHelper(context);
	}
	public void save(Person person)
	{ 
		SQLiteDatabase db=dbopenhelper.getWritableDatabase();
		String sql="insert into person(name,phone,age) values(?,?,?)";
		db.execSQL(sql,new Object[]{person.getName(),person.getPhone(),person.getAge()});
		/*String sql="insert into person(name,phone,age) values('test1','15121045009',23)";
		System.out.println("22222");*/
	}
	public void delete(Integer id)
	{
		SQLiteDatabase db=dbopenhelper.getWritableDatabase();
		//db.execSQL("delete from person where personid=?", new Object[]{id});
		db.execSQL("drop table test.db", null);
	}
	public void update(Person person)
	{
		SQLiteDatabase db=dbopenhelper.getWritableDatabase();
		db.execSQL("update person set name=?,phone=?,age=? where personid=?", new Object[]{person.getName(),person.getPhone(),person.getAge()});
	}
	public Person find(Integer id)
	{
		SQLiteDatabase db=dbopenhelper.getReadableDatabase();
		Cursor cursor=db.rawQuery("select * from person where psersonid=?", new String[]{id.toString()});
		int personid=0;
		String phone="";
		int age=0;
		if(cursor.moveToFirst())
		{
			personid=cursor.getInt(cursor.getColumnIndex("personid"));
			phone=cursor.getString(cursor.getColumnIndex("phone"));
			age=cursor.getInt(cursor.getColumnIndex("age"));
		}
		Person person=new Person();
		person.setPersonid(personid);
		person.setPhone(phone);
		person.setAge(age);
		cursor.close();
		return null;
	}
	public List<Person> getScrollData(int offset,int rows)
	{
		List<Person> person=new ArrayList<Person>();
		SQLiteDatabase db=dbopenhelper.getReadableDatabase();
		System.out.println("eeee");
		Cursor cursor=db.rawQuery("select personid as _id,phone,age from person order by personid desc limit?,?", new String[]{offset+"",rows+""});
		while(cursor.moveToNext())
		{
			int personid=cursor.getInt(cursor.getColumnIndex("_id"));
			String phone=cursor.getString(cursor.getColumnIndex("phone"));
			String name=cursor.getString(cursor.getColumnIndex("name"));
			int age=cursor.getInt(cursor.getColumnIndex("age"));
			Person person_obj=new Person();
			person_obj.setAge(age);
			person_obj.setName(name);
			person_obj.setPersonid(personid);
			person_obj.setPhone(phone);
			person.add(person_obj);
		}
		cursor.close();
		return person;
	}
	public Cursor getCursorScrollData(int offset,int rows)
	{
		SQLiteDatabase db=dbopenhelper.getWritableDatabase();
		Cursor cursor=db.rawQuery("select personid as _id,* from person order by personid desc limit?,?", new String[]{offset+"",rows+""});
		return cursor;
	}
	public long getCount()
	{
		SQLiteDatabase db=dbopenhelper.getReadableDatabase();
		Cursor cursor=db.rawQuery("select count(*) from person",null);
		cursor.moveToFirst();
		long result=cursor.getLong(0);
		cursor.close();
		return result;
		
	}
}
